require("../util.js");
require("../component/components.js");
require("../../jsMt/util.js");
var vm = new Vue({
    el:"#refundInfo",
    data:{
        name:"",
        type:"",
        loginUid:"",
        orderNumber:"",
        afterInfo:{},
        data:"",
        data1:"",
        noticeList:{},//列表
        noticeList1:[],
        noticeList2:[],
        pop:{
            question: "确定取消退款吗？",
            show: false
        },
        logisticsInfo:{},
        courier:""
    },
    mounted:function(){
        this.searchObject = this.getQueryStringArgs();
        this.type=this.searchObject&&this.searchObject.type;
        this.loginUid=this.searchObject&&this.searchObject.uid;
        this.orderNumber=this.searchObject&&this.searchObject.orderNumber;
        this.courier=decodeURI(this.searchObject&&this.searchObject.courier);
        if(this.type==1){
            this.name="退款/售后"
            this.orderAfterSale();
        }else if(this.type==2){
            this.name="退款进度";
            this.orderAfterSale();
        }else if(this.type==3){
            this.name="物流";
            this.logisticsInformation();
        }

    },
    methods:{
        //后退
        getBack:function(){
            // history.go(-1);
            if(this.type==1){
                location.href="/happyi/orderAdmin";
            }else if(this.type==2){
                location.href="/happyi/refundAfterSale";
            }else if(this.type==3){
                history.go(-1);
            }
        },
        bgimg:function(url){
            url = url?url:"";
            return {
                "background-image":'url("'+url+'"),url("")'
            }
        },
        /*获取查询字符对象*/
        getQueryStringArgs: function () {
            //取得查询字符串并去掉开头的问号
            var qs = (location.search.length > 0 ? location.search.substring(1) : ""),
                args = {},
                items = qs.length ? qs.split("&") : [],
                item = null,
                name = null,
                value = null,
                len = items.length;
            for (var i = 0; i < len; i++) {
                item = items[i].split("=");
                name = item[0];
                value = item[1];
                if (name.length) {
                    args[name] = value;
                }
            }
            return args;
        },
         //售后退货进度
        orderAfterSale:function(){
            var that=this;
            var data={
                loginUid:that.loginUid,
                orderNumber:that.orderNumber
            };
            this.ajax({url:"/happyi/orderAfterSale",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.afterInfo=b.json.data;
                    // that.afterInfo.afterState=55;
                }else{
                    vm.$refs.prompt.toast("获取失败！");
                }
            });
        },
        //同意退款
        sellerOrder:function(){
            var that=this;
            var data={
                loginUid:that.loginUid,
                orderNumber:that.orderNumber
            };
            console.log(data)
            this.ajax({url:"/happyi/sellerApprovalOrder",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0){
                    console.log(b.json.data)
                    that.orderAfterSale();
                }else{
                    vm.$refs.prompt.toast("退款失败！");
                }
            });
        },
        //同意退货
        agreeReturnGoods:function(){
            var that=this;
            location.href="/address?type=2&orderNumber="+that.orderNumber;
            // var data={
            //     loginUid:that.loginUid,
            //     orderNumber:that.orderNumber,
            //     addressId:that.afterInfo.addressId
            // };
            // console.log(data);
            // this.ajax({url:"/happyi/agreeReturnGoods",data:data,dataType:"json",method:"post"},function(b){
            //     if(b.json.code==0){
            //         console.log(b.json.data);
            //         that.orderAfterSale();
            //     }else{
            //         vm.$refs.prompt.toast("退货失败！");
            //     }
            // });
        },
        //拒绝退货
        refuseReturnGoods:function(){

        },
        //买家取消退款
        canceluserOrderRefund:function(){
            var that=this;
            var data={
                loginUid:that.loginUid,
                orderNumber:that.orderNumber
            };
            console.log(data);
            this.ajax({url:"/purchase/canceluserOrderRefund",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0){
                    console.log(b.json.data);
                    that.getIsClassify();
                    that.orderAfterSale();
                }else{
                    vm.$refs.prompt.toast("取消退款失败！");
                }
            });
        },
        //买家取消投诉
        canceluserOrderComplaint:function(){
            var that=this;
            var data={
                loginUid:that.loginUid,
                orderNumber:that.orderNumber
            };
            console.log(data);
            this.ajax({url:"/purchase/canceluserOrderComplaint",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0){
                    console.log(b.json.data);
                    vm.$refs.prompt.toast("取消投诉成功！");
                    setInterval(function(){
                        location.href="/happyi/refundAfterSale";
                    },1500);
                    // that.getIsClassify();
                    //that.orderAfterSale();
                }else{
                    vm.$refs.prompt.toast("取消投诉失败！");
                }
            });
        },
        //买家取消退货退款
        canceluserOrderRefundAndGoods:function(){
            var that=this;
            var data={
                loginUid:that.loginUid,
                orderNumber:that.orderNumber
            };
            console.log(data);
            this.ajax({url:"/purchase/canceluserOrderRefundAndGoods",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0){
                    console.log(b.json.data);
                    that.orderAfterSale();
                }else{
                    vm.$refs.prompt.toast("退货失败！");
                }
            });
        },
        //取消退款弹框
        getIsClassify:function(){
            this.pop.show=!this.pop.show;
        },
        getData:function(starData,endData,currentData){
            var sData,eData,sTime,eTime,date3,zData;
            console.log(starData+"---"+endData)
            if(endData!=3){
                sData = starData.replace(/\-/g, "/");
                sTime =new Date(sData); //开始时间
                eData = endData.replace(/\-/g, "/");
                eTime =new Date(eData); //结束时间
                date3=eTime-sTime;
            }else{
                sData = currentData.replace(/\-/g, "/");
                sTime =new Date(sData); //开始时间
                zData = starData.replace(/\-/g, "/");
                eTime =new Date(zData).getTime()+1000*60*60*24*parseInt(3);
                date3=eTime-sTime;
            }
            //计算出相差天数
            var days=Math.floor(date3/(24*3600*1000));
            console.log(days)
            //计算出小时数
            var leave1=date3%(24*3600*1000) ;   //计算天数后剩余的毫秒数
            var hours=Math.floor(leave1/(3600*1000));
            if(endData!=3){
                //计算相差分钟数
                var leave2=leave1%(3600*1000) ;      //计算小时数后剩余的毫秒数
                var minutes=Math.floor(leave2/(60*1000));
                // //计算相差秒数
                // var leave3=leave2%(60*1000);      //计算分钟数后剩余的毫秒数
                // var seconds=Math.round(leave3/1000);
                if(days>=0&&hours>=0&&minutes>=0){
                    return days+"天"+hours+"小时"+minutes+"分钟";
                }else{
                    return "时间过期";
                }

            }else{
                if(days>=0&&hours>=0){
                    return days+"天"+hours+"小时";
                }else{
                    return "时间过期";
                }
            }

        },
        getNext:function(num){
            if(num==1){  //拒绝退款
                console.log(num)
                location.href="/happyi/refuseRefund?type=1&uid="+this.loginUid+"&orderNumber="+this.orderNumber;
            }else if(num==2){   //订单详情
                if(this.type==1){
                    location.href="/happyi/orderInfo?type=1&uid="+this.loginUid+"&orderNumber="+this.orderNumber;
                }else if(this.type==2){
                    location.href="/purchase/myOrderInfo?type=1&orderNumber="+this.orderNumber+"&uid="+this.loginUid;
                }
            }else if(num==3){  //买家投诉
                location.href="/happyi/refuseRefund?type=2&uid="+this.loginUid+"&orderNumber="+this.orderNumber;
            }else if(num==4){  //买家举证
                location.href="/happyi/refuseRefund?type=3&uid="+this.loginUid+"&orderNumber="+this.orderNumber;
            }else if(num==5){  //卖家举证
                location.href="/happyi/refuseRefund?type=4&uid="+this.loginUid+"&orderNumber="+this.orderNumber;
            }else if(num==6){  //买家填写物流
                location.href="/happyi/deliverGoods?type=2&uid="+this.loginUid+"&orderNumber="+this.orderNumber;
            }else if(num==7){
                location.href="/purchase/applyRefund?type=1&uid="+this.loginUid+"&orderNumber="+this.orderNumber;
            }else if(num==8){
                location.href="/purchase/applyRefund?type=2&uid="+this.loginUid+"&orderNumber="+this.orderNumber
            }else if(num==9){  //拒绝退货
                location.href="/happyi/refuseRefund?type=5&uid="+this.loginUid+"&orderNumber="+this.orderNumber
            }else if(num==10){
                location.href="/happyi/refundInfo?type=3&orderNumber="+this.afterInfo.courierNumber+"&uid="+this.loginUid+"&courier="+encodeURI(this.afterInfo.courier)
            }
        },
        //物流信息
        logisticsInformation:function(){
            var that=this;
            var data={
                loginUid:that.loginUid,
                // courierNumber:"70287911122392",
                // courier:"百世汇通"
                courier:that.courier,  //物流公司
                courierNumber:that.orderNumber   //快递单号
            };
            console.log(data);
            this.ajax({url:"/purchase/logisticsInformation",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.logisticsInfo=JSON.parse(b.json.data);
                    console.log(that.logisticsInfo)
                }else{
                    vm.$refs.prompt.toast("获取物流信息失败！");
                }
            });
        }
    }
    ,watch:{
        afterInfo:function(){
            var that=this;
            that.$nextTick(function(){
                if(that.afterInfo.afterState==10||that.afterInfo.afterState==60){
                    that.data=that.getData(that.afterInfo.currentTime,that.afterInfo.limitDate);
                }else if(that.afterInfo.afterState==50||that.afterInfo.afterState==100||that.afterInfo.afterState==110){
                    that.data1=that.getData(that.afterInfo.complaintDate,3,that.afterInfo.currentTime);
                }
                getLive();
            });
        },
        logisticsInfo:function(){
            var that=this;
            that.$nextTick(function(){
                getLive();
            });
        }
    }
});
function getLive(){
    var canvas = $("#canvas");
    console.log($(".listB:first"))
    console.log($(".listB:last"))
    var top=$(".listB:first").offset().top;
    var top1=$(".listB:last").offset().top;
    console.log(top+"==="+top1);
    if(top!=top1){
        canvas.css({"height":top1-top});
    }else{
        canvas.css({"height":140});
    }
}